// TypeScript file
class PuKeMenager {
    private static puke: PuKeMenager;
    public static Puke(): PuKeMenager {
        if(PuKeMenager.puke == null)
            PuKeMenager.puke = new PuKeMenager();
        return PuKeMenager.puke;
    }
    
    private wangfa:string=egret.localStorage.getItem("wangfa")//n：无大小王；y:有大小王
    // public static uer_number:number//参与本局的人数

    //发牌
    // public get FaPai():any[]{
    //     var cords:any[]=new Array();//一副牌数组
    //     var randcords:any[]=new Array();//发出来的牌
    //     if(this.wangfa=="y"){
    //         for(var i=0;i<54;i++){
    //             cords[i]=i+1
    //         }
    //         for(var i=0;i<PuKeMenager.uer_number*4;i++){//发4张随机牌
    //         var cordnum=Math.round(Math.random()*(53-i));//0到53中的随便一个数，即代表54张牌中的随便一张牌
    //         randcords[i]=cords[cordnum];//取出代表54张牌的数组的一个，即取出一张牌放到randcords数组
    //         cords.splice(cordnum,1);//将选出的牌从纸牌数组中剔除
    //         }
    //     }else{
    //         for(var i=0;i<52;i++){
    //             cords[i]=i+1
    //         }
    //         for(var i=0;i<PuKeMenager.uer_number*4;i++){//发4张随机牌
    //         var cordnum=Math.round(Math.random()*(51-i));//0到51中的随便一个数，即代表52张牌中的随便一张牌
    //         randcords[i]=cords[cordnum];//取出代表52张牌的数组的一个，即取出一张牌放到randcords数组
    //         cords.splice(cordnum,1);//将选出的牌从纸牌数组中剔除
    //         }
    //     }
    //     return randcords;
    // }


    public static copyArray(array){
        var result={};
        for (var key in array) {
            result[key] = typeof array[key]==='object'? this.copyArray(array[key]): array[key];
        } 
        return result; 
    }

    public static four(cards:any[],card:string):number{//输入4张牌的数组和其中一个元素，输出该元素的index
        for(var i=0;i<cards.length;i++){
            if(<string>cards[i]==card){
                return i+1;
            }
        }
        return -1;
    }

    public paixu(card:any[]):any[]{//输入一个数字数组，输出从小到大的数组
        for(var i=0;i<card.length;i++){
            for(var k=0;k<card.length-1;k++){
                if(card[k]>card[k+1]){var a=card[k];card[k]=card[k+1];card[k+1]=a }
            }
        }
        return card
    }
    //输入4张牌的数组，返回应该废掉牌的index
    public TiShi(cards:any[]):number{
        var card=[];
        for(var k=0;k<cards.length;k++){
            card.push(cards[k]);
        }
        //从小到大
        card=this.paixu(card);
        var one:number =(card[0])/4;var two:number = (card[1])/4;var three:number = (card[2])/4;var four:number = (card[3])/4;//获得点数
        one= Math.ceil(one);two= Math.ceil(two);three= Math.ceil(three);four= Math.ceil(four);//向上取整,1~13点代表A~K,14代表大小王
        // console.log(card,one,two,three,four);
        if(four>13){
            if(three>13){//有两张王
                if(one==two){//另外两张牌相同，去掉鬼牌
                    return PuKeMenager.four(cards,card[3])
                }else{//另外两张牌不同，去掉点数小的
                    if(one==3){return PuKeMenager.four(cards,card[1])}
                    if(two==3){return PuKeMenager.four(cards,card[0])}
                    return PuKeMenager.four(cards,card[0])
                }
            }else{//有一张王
                if(one==two){
                    if(two==three){//另外三张相同,去掉王牌
                        return PuKeMenager.four(cards,card[3])
                    }else{//前面两张相同，去掉不同的牌
                        return PuKeMenager.four(cards,card[2])
                    }
                }else{
                    //后面两张相同或不同都去掉最小的牌
                    return PuKeMenager.four(cards,card[0])
                }
            }
        }else{//没有王
            if(four==one || two==four){//4张相同或后3张相同,去掉最小的那张牌
                return PuKeMenager.four(cards,card[0])
            }else{
                if(one==three){//前3张相同,去掉不同的那张牌
                    return PuKeMenager.four(cards,card[3])
                }else{
                    //没有3张相同的情况下分有三公牌型和无三公牌型
                    if(one>10 || two>10){//有三公牌型就去掉最小牌
                        return PuKeMenager.four(cards,card[0])
                    }else{//没有三公,判断有没有金花
                        var huase1=0,huase2=0,huase3=0,huase4=0;//方片，梅花，红桃，黑桃
                        for(var i=0;i<4;i++){
                            if((card[i])%4==1){huase1++;}
                            else if((card[i])%4==2){huase2++;}
                            else if((card[i])%4==3){huase3++;}
                            else{huase4++;}
                        }
                        if(huase1<3 && huase2<3 && huase3<3 && huase4<3){//没有金花
                            if(this.BiJinHua([card[0],card[1],card[2]])[1]==1 && this.BiJinHua([card[1],card[2],card[3]])[1]==1){//金花牌型是散牌则判断三公牌型,如果有8点以上牌型就去掉余下的一张否则就去掉点数最小的
                                var maxquanzhi:any[]=this.paixu([this.SanGong([card[0],card[1],card[2]])[2],this.SanGong([card[0],card[1],card[3]])[2],this.SanGong([card[0],card[2],card[3]])[2],this.SanGong([card[1],card[2],card[3]])[2]])
                                if(this.SanGong([card[0],card[1],card[2]])[2]==maxquanzhi[3]){return PuKeMenager.four(cards,card[3])}
                                else if(this.SanGong([card[0],card[1],card[3]])[2]==maxquanzhi[3]){return PuKeMenager.four(cards,card[2])}
                                else if(this.SanGong([card[0],card[2],card[3]])[2]==maxquanzhi[3]){return PuKeMenager.four(cards,card[1])}
                                else{return PuKeMenager.four(cards,card[0])}
                            }else{ //金花牌型不是散牌则可能是对子或顺子
                                if(this.BiJinHua([card[0],card[1],card[2]])[2]>this.BiJinHua([card[1],card[2],card[3]])[2]){//这四张牌是按点数从小到大拍的，把前三张和后三张的权值比较，废弃掉权值较小的三张牌中点数最小的牌
                                    return PuKeMenager.four(cards,card[3])
                                }else{
                                    return PuKeMenager.four(cards,card[0])
                                }
                            }  
                        }else{//至少有三张同花就将不是同花的废掉
                            if(huase1>2){//有至少三张花色为huase1的牌
                                for(var i=0;i<4;i++){
                                    if((card[i])%4==1){
                                        continue
                                    }else{
                                        return PuKeMenager.four(cards,card[i])
                                    }
                                }
                            }else if(huase2>2){//有至少三张花色为huase2的牌
                                for(var i=0;i<4;i++){
                                    if((card[i])%4==2){
                                        continue
                                    }else{
                                        return PuKeMenager.four(cards,card[i])
                                    }
                                }
                            }else if(huase3>2){//有至少三张花色为huase3的牌
                                for(var i=0;i<4;i++){
                                    if((card[i])%4==3){
                                        continue
                                    }else{
                                        return PuKeMenager.four(cards,card[i])
                                    }
                                }
                            }else{//有至少三张花色为huase4的牌
                                for(var i=0;i<4;i++){
                                    if((card[i])%4==0){
                                        continue
                                    }else{
                                        return PuKeMenager.four(cards,card[i])
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    //输入3张牌返回3张牌的金花牌型,返回【牌型，倍数，权值】
    public BiJinHua(card:any[]):any[]{
        //从小到大
        card=this.paixu(card)
        var one:number =(card[0])/4;var two:number = (card[1])/4;var three:number = (card[2])/4;
        one= Math.ceil(one);two= Math.ceil(two);three= Math.ceil(three);//获得点数,1~13点代表A~K,14代表大小王
       if(three==14){
           //若有鬼牌则还需减5
            if(two==14){//有两个王
                return ["baozi",6,one*1000000+(card[0])%4-5];//权值=点数*牌型倍数+花型-5
            }else{//有一个王
                if(one==two){//对子
                    return ["baozi",6,one*100+(card[0])%4-5];
                }else if(two-one==1){
                    if((card[0])%4 == (card[1])%4){//金花顺
                        return ["jinhuashun",5,three*100000+(card[2])%4-5];
                    }else{//顺子
                         return ["shunzi",3,three*1000+(card[2])%4-5];
                    }
                }else{//散牌
                    return ["sanpai",1,three*10+(card[2])%4-5];
                }
            }
        }else{//没有王，因为三张牌按大小排序，如果最大的一张不是王则没王
            //判断是否为点数一样
                if(one == two || two == three){
                    if(one == two && two == three){//（豹子）
                        return ["baozi",6,one*1000000+(card[0])%4];
                    }else{
                        if(one == two){return ["duizi",2,one*100+(card[0])%4];}
                        if(two == three){return ["duizi",2,three*100+(card[2])%4];}
                    }
                }
            //判断是否为花色一样（金花）
            if((card[0])%4 == (card[1])%4 && (card[1])%4 == (card[2])%4){
                    return ["jinhua",4,three*10000+(card[2])%4];
            }
            //判断是否为顺子(顺子)
            if((three-two==1 && two-one==1) || (three-one==-12 && three-two==1)){
                //判断是否为花色一样（金花顺）
                if((card[0])%4 == (card[1])%4 && (card[1])%4 == (card[2])%4){
                    return ["jinhuashun",5,three*100000+(card[2])%4];
                }else{
                    return ["shunzi",3,three*1000+(card[2])%4];
                }
            }
            return ["sanpai",1,three*10+(card[2])%4];
        }
    }
     //输入3张牌返回3张牌的三公牌型,返回【牌型，倍数，权值】
    public SanGong(card:any[]):any[]{
        //从小到大
        card=this.paixu(card)
        var one:number =(card[0])/4;var two:number = (card[1])/4;var three:number = (card[2])/4;
        one= Math.ceil(one);two= Math.ceil(two);three= Math.ceil(three);//获得点数,1~13点代表A~K,14代表大小王

        if(three==14){
            if(two==14){//有两个王
                if(one==3){//爆三
                    return ["baosan",6,one*1000000+(card[0])%4-5]
                }else{//三条
                    return ["santiao",5,one*100000+(card[0])%4-5]
                }
            }else{//有一个王
                if(one==two){//对子
                    if(one==3){//爆三
                        return ["baosan",6,one*1000000+(card[1])%4-5]
                    }else{//三条
                        return ["santiao",5,one*100000+(card[1])%4-5]
                    }
                }else{
                   if(one>10){//三公
                       return ["sangong",4,three*10000+(card[1])%4-5]
                   }else{//9点
                       return ["jiudian",3,three*1000+(card[1])%4-5]
                   }
                }
            }
        }else{//没有王，因为三张牌按大小排序，如果最大的一张不是王则没王
            //判断三条
            if(one==two && two==three){
                if(one==3){//判断爆三
                    return ["baosan",6,one*1000000+(card[2])%4]
                }
                return ["santiao",5,one*100000+(card[2])%4]
            }
            //判断三公
            if(one>10){
                return ["sangong",4,three*10000+(card[2])%4]
            }
            var three2=three
            if(one>9){one=0}
            if(two>9){two=0}
            if(three>9){three2=0}
            var san=one+two+three2;
            san=san-(Math.floor(san/10))*10
            if(san == 0){return ["lingdian",1,three*10+(card[2])%4]}
            if(san == 1){return ["yidian",1,three*10+(card[2])%4]}
            if(san == 2){return ["liangdian",1,three*10+(card[2])%4]}
            if(san == 3){return ["sandian",1,three*10+(card[2])%4]}
            if(san == 4){return ["sidian",1,three*10+(card[2])%4]}
            if(san == 5){return ["wudian",1,three*10+(card[2])%4]}
            if(san == 6){return ["liudian",1,three*10+(card[2])%4]}
            if(san == 7){return ["qidian",1,three*10+(card[2])%4]}
            if(san == 8){return ["badian",2,three*100+(card[2])%4]}
            if(san == 9){return ["jiudian",3,three*1000+(card[2])%4]}
        }
    }
    
}